package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.RolePermission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@DubboService
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public Permission findById(Integer id) {
        Permission newPermission = new Permission();
        newPermission.setId(id);
        Permission permission = permissionDao.selectOne(newPermission);
        log.info("[permission表-findById-返回值] - permission: {}",permission);
        return permission;
    }

    @Override
    public List<Permission> findAll() {
        List<Permission> permissionList = permissionDao.selectAll();
        log.info("[permission表-findAll-返回值] - permissionList: {}",permissionList);
        return permissionList;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[permission表-findPage-接收值] - queryPageBean: {}",queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> page = permissionDao.findPage(queryPageBean.getQueryString());
        log.info("[permission表-findPage-返回值] - page: {}",page);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public boolean add(Permission permission) {
        log.info("[permission表-add-接收值] - permission: {}",permission);
        boolean flag = false;
        int insertCount = permissionDao.insertSelective(permission);
        if (insertCount>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean update(Permission permission) {
        log.info("[permission表-update-接收值] - permission: {}",permission);
        boolean flag = false;
        int updateCount = permissionDao.updateByPrimaryKeySelective(permission);
        if (updateCount > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean deleteById(Integer id) {
        log.info("[permission表-delete-接收值] - id: {}",id);
        boolean flag = false;
        int deleteCount = permissionDao.deleteByPrimaryKey(id);
        if (deleteCount>0){
            flag = true;
        }
        return flag;
    }
}
